<template>
    <div>
        <el-form label-width="50px" :model="inboundOrder" ref="inboundOrder_refs">
            <el-row>
                <el-col :span="24">
                    <el-table border stripe  :data="tableData">
                        <el-table-column label="名称" prop="DeviceName" align="center"></el-table-column>
                        <el-table-column label="规格型号" prop="DeviceModel" align="center"></el-table-column>
                        <el-table-column label="数量" prop="Amount" align="center">
                            <template slot-scope="slot">
                                <el-input v-model="slot.row.Amount"></el-input>
                            </template>
                        </el-table-column>
                        <el-table-column label="单位" prop="Unit" align="center"></el-table-column>
                        <el-table-column label="单价" prop="Price" align="center">
                            <template slot-scope="slot">
                                <el-input v-model="slot.row.Price"></el-input>
                            </template>
                        </el-table-column>
                        <el-table-column label="总价" prop="TotalPrice"  align="center">
                            <template slot-scope="slot">
                                {{ slot.row.Amount * slot.row.Price }}
                            </template>
                        </el-table-column>
                        <el-table-column label="操作" align="center">
                            <template slot-scope="slot">
                                <el-button icon="el-icon-delete" size="mini" type="danger" @click="TableRemove_event(slot.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-col>
            </el-row>
            <el-row style="margin-top: 10px">
                <el-col :span="24">
                    <el-form-item label="备注">
                        <el-input v-model="inboundOrder.Remark"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row style="margin-top: 10px">
                <el-col :span="24" style="text-align: center">
                    <el-button type="primary" @click="AddNewMaterial_dialog=true">新建物资</el-button>
                    <el-button type="warning" @click="AddExistingMaterial_dialog=true">导入物资</el-button>
                    <el-button type="success" @click="Submit_event">确认</el-button>
                </el-col>
            </el-row>
        </el-form>

        <el-dialog :visible.sync="AddNewMaterial_dialog" width="80%" top="10px"  :modal="false" title="新建物资" @close="AddNewMaterial_dialog_dlosed">
            <!--            <AddNewMaterial :device="Device" :all-department="AllDepartment" :all-device-category="AllDeviceCategory" :all-device-state="AllDeviceState"></AddNewMaterial>-->
            <AddNewMaterial ref="AddNewMaterial_ref" :all-department="AllDepartment" :all-device-category="AllDeviceCategory" :all-device-state="AllDeviceState" :get-new-device="GetNewDevice"></AddNewMaterial>
        </el-dialog>

        <el-dialog :visible.sync="AddExistingMaterial_dialog" :modal="false" width="90%" top="10px" title="现有物资导入">
            <AddExistingMaterial :is-clear-id="false" :selection_-material="tableData" :all-device-state="AllDeviceState" :all-device-category="AllDeviceCategory" :all-department="AllDepartment"></AddExistingMaterial>
        </el-dialog>
    </div>
</template>

<script>
    import AddNewMaterial from "./AddNewMaterial";
    import AddExistingMaterial from "./AddExistingMaterial";

    export default {
        name: "MaterialStorage",
        props: {
            AllDeviceCategory: {
                type: Array,
                required: true
            },
            AllDepartment: {
                type: Array,
                required: true
            },
            AllDeviceState: {
                type: Array,
                required: true
            }
        },
        components: {
            'AddNewMaterial': AddNewMaterial,
            'AddExistingMaterial': AddExistingMaterial,
        },
        data() {
            return {
                remarks: '',
                Device: {
                    DeviceNumber: '',
                    DeviceCategoryIdOne: 0,
                    DeviceCategoryIdTwo: 0,
                    DeviceModel: '',
                    DeviceName: '',
                    ReferenceWebsite: '',
                    DepartmentId: 0,
                    PurchaseCause: '',
                    Price: '',
                    PurchaseDate: new Date(),
                    State: 0,
                    Pinyin: '',
                    Amount: 0,
                    TotalPrice: 0,
                    Unit: '',
                    StockAmount: 0,
                    StockPrice: 0,
                    DamageAmount: 0,
                    DamagePrice: 0,
                    ConsumeAmount: 0,
                    ConsumePrice: 0,
                    Operator: 0,
                    imgUrl: '',
                },
                tableData: [],
                //新建物资页面的显示
                AddNewMaterial_dialog: false,
                AddExistingMaterial_dialog: false,
                inboundOrder: {
                    Remark: '',
                },
            }
        },
        methods: {
            //表格数据移除
            TableRemove_event(obj) {
                this.tableData.forEach((item, i) => {
                    if (item.Id == obj.Id) {
                        this.tableData.splice(i, 1)
                    }
                })
            },
            //关闭新建物资弹窗的回调
            AddNewMaterial_dialog_dlosed() {
                this.$refs.AddNewMaterial_ref.Fromreset()
            },
            //物资提交确认
            Submit_event() {
                if (this.tableData.length <= 0) {
                    this.$message.warning('没有选定的物资!')
                    return;
                }
                for (let i = 0; i < this.tableData.length; i++) {
                    if (this.tableData[i].Amount == "0" || this.tableData[i].Price == "0") {
                        this.$message.warning('物品关键信息不能为0')
                        return;
                    }
                }
                let loadding = this.openLoading();
                this.$http.post('Device/AddInboundOrderContent', {
                    inboundOrder: this.inboundOrder,
                    devices: this.tableData
                }).then(res => {
                    loadding.close();
                    if (res.data.msg.IsSuccess) {
                        this.$message.success('添加成功')
                        this.$parent.$parent.Material_storage_dialog = false
                    } else {
                        this.$message.warning('添加失败')
                    }
                })
            },
            //窗体关闭表格处理
            dialog_close() {
                // 重置表单数据
                this.$refs['inboundOrder_refs'].resetFields()
                this.tableData = []
            },
            //新建物资的同步添加
            GetNewDevice(obj) {
                let new_obj = JSON.parse(JSON.stringify(obj))
                this.tableData.push(new_obj)
                this.AddNewMaterial_dialog = false
            }
        }
    }
</script>

<style scoped>

</style>
